from django.shortcuts import render
from django.http import HttpResponse

from . import models

def index(request):
    # return HttpResponse('HELLO, world<br/> E:\python3\myblog\myblog1\ blog\ views.py')
    #Article 数据表名
    article = models.Article.objects.get(pk=1)
    return render(request, 'blog/index.html',{'hello':article,'answer':'从后台传来'})

def index2(request):
    #获取Article表的全部数据 向前台渲染 all 是全部取出
    articles = models.Article.objects.all()
    return render(request, 'blog/index2.html',{'articles':articles})

def article_page(request,article_id):
    article = models.Article.objects.get(pk=article_id)
    return render(request,'blog/article_page.html',{'article':article})

def text(request):
    data = 'article_id'
    return HttpResponse(data)

def params_first(request):
    #http://127.0.0.1:8000/blog/args/?user=张飒&pwd=123
    user_name=request.GET.get('user','')
    pass_word=request.GET.get('pwd','')
    result='账号:{},密码:{}'.format(user_name,pass_word)
    return HttpResponse(result)

def params(request,name,pwd):
    #http://127.0.0.1:8000/blog/params/张发给/123/
    result='name={},pwd={}'.format(name,pwd)
    return HttpResponse(result)

def params1(request,username,password):
    #http://127.0.0.1:8000/blog/params1/张发给/1423/
    result='name={},pwd={}'.format(username,password)
    return HttpResponse(result)

def index3(request):
    #获取Article表的全部数据 向前台渲染 all 是全部取出
    articles = models.Article.objects.all()
    return render(request, 'blog/index3.html',{'articles':articles})

def index4(request,article_id):
    #result='id={}'.format(article_id);
    id = format(article_id)
    article = models.Article.objects.get(pk=id)
    return render(request, 'blog/index.html', {'hello': article, 'answer': '从index4的后台传来'})

#编辑页面
def edit_page(request):
    return render(request,'blog/edit_page.html')

#收到表单提交的数据 传给数据库

def edit_action(request):
    title = request.POST.get('title','默认值：标题')
    content = request.POST.get('content','默认值：内容')
    models.Article.objects.create(title=title,content=content)
    articles = models.Article.objects.all()
    return render(request,'blog/index.html',{'articles':articles})

def index5(request):
    data = '函数index5 用show.html 页面 但是由于定义了path为 shiyan 所以 地址栏要写http://127.0.0.1:8000/blog/shiyan/'
    return render(request, 'blog/show.html',{'data':data})
















